package com.example.service;

import com.example.entity.Student;
import com.example.mongodb.StudentMongo;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;


@Service
public class ServiceMongoImpl implements StudentMongo {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean students(Student student) {
        return true;
    }

    public List<Student> students() {
        Criteria id = Criteria.where("studentId").is("1");
        Query query = new Query(id);
        List<Student> students = mongoTemplate.find(query, Student.class);
        return students;
    }

    public Boolean deleteStudent(String value, Class c) {
        Criteria studentId = Criteria.where("studentId").is(value);
        Query query = new Query(studentId);
        DeleteResult remove = mongoTemplate.remove(query, c);
        System.out.println(remove.getDeletedCount());
        System.out.println(remove.wasAcknowledged());
        if (remove != null) {
            return true;
        } else {
            return false;
        }
    }

    public Boolean insert() {
        List<Student> students = new ArrayList<>();
        for (Integer i = 0; i < 3; i++) {
            Student student = new Student(i.toString(), i.toString(), i, i.doubleValue(), LocalDateTime.now(),LocalDateTime.now());
            students.add(student);
        }
        Collection<Student> insert = mongoTemplate.insert(students, Student.class);
        if (insert.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public Student student(String key) {
        Criteria id = Criteria.where("id").is(key);
        Query query = new Query(id);
        Student one = mongoTemplate.findOne(query, Student.class);
        if (one == null) {
            return null;
        } else {
            return one;
        }
    }

    public List<Student> manyCondition(String key, String key2) {
//        Criteria criteria = new Criteria().orOperator(Criteria.where("id").is(key2),
//                Criteria.where("studentName").is(key));
        Criteria criteria = Criteria.where("id").ne(key).orOperator(Criteria.where("studentName").ne(key2));
        Query query = new Query(criteria);
        List<Student> students = mongoTemplate.find(query, Student.class);
        if (students == null) {
            return null;
        } else {
            return students;
        }
    }

    public List<Student> inQuery(List<String> ids) {
        Criteria id = Criteria.where("id").in(ids);
        Query query = new Query(id);
        List<Student> students = mongoTemplate.find(query, Student.class);
        return students;
    }

    public List<Student> sectionStudent(int val){
        Criteria id = Criteria.where("studentAge").gte(val);
        Query query = new Query(id);
        List<Student> students = mongoTemplate.find(query, Student.class);
        return students;
    }

    public List<Student> regexStudent(String time){
        Criteria id = Criteria.where("studentName").regex(time);
        Query query = new Query(id);
        List<Student> students = mongoTemplate.find(query, Student.class);
        return students;
    }

    public List<Student> statisticsStudentCount(String time,String val){
        Criteria id = Criteria.where("id").not();
        Query query = new Query(id);
        List<Student> students = mongoTemplate.find(query, Student.class);
        long count = mongoTemplate.count(query, Student.class);
        System.out.println("学生总数="+count);
        return students;
    }

    public List<Map> groupStudent(){
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.group("studentAge").avg("id").as("age"));
        AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, Student.class, Map.class);
        List<Map> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }

    public List<Student> sortStudent(){
        Criteria studentAge = Criteria.where("studentAge").gt(-1);
        Query query = new Query(studentAge);
        List<Student> students = mongoTemplate.find(query, Student.class);
        return students;
    }

    public Boolean saveRecords(List<Student> student,String collectionName){
        Collection<Student> save = mongoTemplate.insert(student,collectionName);
        if(Objects.isNull(save)){
            return false;
        }else {
            return true;
        }
    }
    public Boolean batchDel(Integer val){
        Criteria id = Criteria.where("studentAge").gte(val);
        Query query = new Query(id);
        DeleteResult student = mongoTemplate.remove(query, Student.class, "student");
        if(student.getDeletedCount()>0){
            return true;
        }else {
            return false;
        }
    }
    public Boolean modifiedStudent(String val){
        Criteria id = Criteria.where("id").is(val);
        Query query = new Query(id);
        Update update = Update.update("studentScore",88.0);
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Student.class);
        long modifiedCount = updateResult.getModifiedCount();
        if(modifiedCount>0){
            return true;
        }else {
            return false;
        }
    }

    public Boolean paginationStudent(Integer val){
        PageRequest studentScore = PageRequest.of(0, 5, Sort.by("studentScore"));
        Query query = new Query();
        query.with(studentScore);
        List<Student> students = mongoTemplate.find(query, Student.class);
        if(students!=null){
            for (Student student : students) {
                System.out.println(student);
            }
            return true;
        }else {
            return false;
        }
    }

    public Boolean indxs(){
        IndexOperations indexOperations = mongoTemplate.indexOps(Student.class);
        System.out.println(indexOperations.getIndexInfo());
        return true;
    }
}
